package cn.lhx.blog.action;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import cn.lhx.blog.formbean.CategoryFormBean;
import cn.lhx.blog.pojo.Article;
import cn.lhx.blog.pojo.ArticleCategory;
import cn.lhx.blog.service.ArtiCatgService;
import cn.lhx.blog.service.ArticleService;
import cn.lhx.blog.util.BlogInfo;
import cn.lhx.blog.util.PagenationTookit;

/**
 * 执行分类管理操作
 */
public class CategoryManager extends DispatchAction {
	@Resource
	private ArtiCatgService artiCatgService;
	@Resource
	private ArticleService articleService;

	@Override
	protected ActionForward dispatchMethod(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response, String name) throws Exception {
		log.info("派遣方法  method= " + request.getParameter("method"));
		if (null == name) {
			// 默认是查看该类型的日志
			return viewArticlesByCatgId(mapping, form, request, response);
		} else {
			return super.dispatchMethod(mapping, form, request, response, name);
		}

	}

	/**
	 * 取出分类信息
	 * 
	 * @param mapping
	 * @param arg1
	 * @param request
	 * @param arg3
	 * @return
	 * @throws Exception
	 */
	public ActionForward loadCategories(ActionMapping mapping, ActionForm arg1,
			HttpServletRequest request, HttpServletResponse arg3)
			throws Exception {
		// 开始获取分类信息
		List<ArticleCategory> artCatgList = artiCatgService.getAllCategory();
		request.setAttribute("categories", artCatgList);
		// 此时还没有加载包含的文章
		log.info("获取日志分类，此时还没有加载包含的文章");
		return mapping.findForward("categories_manager");

	}

	/**
	 * 保存分类信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward save(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CategoryFormBean ctgf = (CategoryFormBean) form;
		String name = ctgf.getName();
		String desc = ctgf.getDescription();

		ArticleCategory artiCatg = new ArticleCategory();
		artiCatg.setName(name);
		artiCatg.setDescription(desc);
		// 保存分类信息
		artiCatgService.saveArtiCatg(artiCatg);

		System.out.println("save Category!");

		return mapping.findForward("saveOK");

	}

	/**
	 * 删除分类信息
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		CategoryFormBean ctgf = (CategoryFormBean) form;
		int _id = ctgf.getId();
		// 删除分类
		artiCatgService.delArticatg(_id);
		System.out.println("CategoryManager In !! id= " + _id);
		return mapping.findForward("deleteOK");
	}

	/**
	 * 取出对应类型的所有日志,使用了延时加载
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward viewArticlesByCatgId(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		CategoryFormBean ctgf = (CategoryFormBean) form;
		int _id = ctgf.getId();
		// 使用了延时加载：先只加载改分类，然后在view上加载所含的日志
		ArticleCategory catg = artiCatgService.getCategoryById(_id);
		request.setAttribute("catg", catg);
		
		HttpSession session=request.getSession();
		boolean isAdmin=false;
		if(session.getAttribute("adminIsLogin")!=null){
			isAdmin=true;
		}
		//前台和后台显示的配置不一样
		int blogsOnePage= isAdmin? BlogInfo.ADMIN_BLOGS_ONE_PAGE:BlogInfo.BLOGS_ONE_PAGE;
		int pageNumbers= isAdmin? BlogInfo.ADMIN_CUNT_OF_PAGENUMBER:BlogInfo.COUNT_OF_PAGENUMBER_OF_MAINPAGE;
		// 获取当前页码
		int page = ctgf.getPage();
		page = page < 1 ? 1 : page;
		// 获得总共有多少篇日志
		long articleCount = catg.getIncludeArticles().size();
		// 计算出页数(pages)=articleCount/6
		int pages = PagenationTookit.getPages(blogsOnePage, articleCount);
		page = page > pages ? pages : page;
		log.info("page= " + page);
		request.setAttribute("pages", pages);
		request.setAttribute("curPage", page);
		// 设置页码导航条
		int nav[] = PagenationTookit.getPageNavigator(page,
				pageNumbers, pages);
		request.setAttribute("nav", nav);
		int base = page - 1;// 基点=第n页 -1
		long startIndex=0;
		long endIndex=0;
			startIndex=base*blogsOnePage;
			log.info("start = "+startIndex);
			//求字列表结束Index
			endIndex=PagenationTookit.getEndIndex(startIndex, blogsOnePage, articleCount);
//	求子列表
//		List<Article> articles=catg.getIncludeArticles().subList((int)startIndex, (int)endIndex);
//		request.setAttribute("articles", articles);
		//传索引
		request.setAttribute("startIndex", startIndex);
		request.setAttribute("endIndex", endIndex);
		log.info("startIndex= "+startIndex);
		log.info("endIndex= "+endIndex);
		
		log.info("获得该分类ID=" + _id + "的日志列表");
		
		if(isAdmin){//是管理员登陆的话就跳转到管理界面，否则跳到前台
			return mapping.findForward("show_catgBlogs");
		}
		return mapping.findForward("front_show_catgBlogs");
		
	}

}
